qemu := /root/qemu/qemu-8.2.2/build/qemu-system-arm

bin ?= hello

ifeq ($(bin),device)
	cpu := cortex-m4
	machine := netduinoplus2  
	target := thumbv7em-none-eabihf
	device_cargo := echo "\033[41m\033[1mCargo.toml needs stm32f3 dependency for device example: uncomment it\033[0m"
else
	cpu := cortex-m3
	machine := lm3s6965evb  
	target := thumbv7m-none-eabi
  device_cargo :=
endif

build:
	@$(device_cargo)
	cargo build --example $(bin) --target $(target)

run:
	$(qemu) \
  -cpu $(cpu) \
  -machine $(machine) \
  -nographic \
  -semihosting-config enable=on,target=native \
  -kernel target/$(target)/debug/examples/$(bin)

gdbserver:
	$(qemu) \
  -cpu $(cpu) \
  -machine $(machine) \
  -nographic \
  -semihosting-config enable=on,target=native \
  -gdb tcp::3333 \
  -S \
  -kernel target/$(target)/debug/examples/$(bin)


GDB_PATH := /root/qemu/gdb-14.2/build-arm-eabi/bin/arm-none-eabi-gdb
gdb := RUST_GDB=$(GDB_PATH) rust-gdb

gdbclient:
	$(gdb) -ex 'file target/$(target)/debug/examples/$(bin)' -ex 'target remote :3333'
